package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.UserLocation;
import com.itheima.service.mongo.UserLocationService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class UserLocationServiceImpl implements UserLocationService {


    @Autowired
    MongoTemplate mongoTemplate;


    @Override
    public void save(Long userId,Double longitude,Double latitude,String addrStr) {
        //1.创建条件对象(userId)
        Query query = new Query(Criteria.where("userId").is(userId));

        //2.查询userLocation表获取对象
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);

        //3.判断对象是否为空
        if (userLocation == null) {
            //3.1 若为空,就是保存操作
            userLocation = new UserLocation();
            userLocation.setUserId(userId);
            //先写经再写纬
            userLocation.setLocation(new GeoJsonPoint(longitude,latitude));

            userLocation.setAddress(addrStr);
            userLocation.setCreated(System.currentTimeMillis());
            userLocation.setUpdated(System.currentTimeMillis());

            mongoTemplate.save(userLocation);
        }else{
            //3.2 若不为空,就是更新操作
            userLocation.setLocation(new GeoJsonPoint(longitude,latitude));//先写经再写纬
            userLocation.setAddress(addrStr);
            //更新"上次更新时间"
            userLocation.setLastUpdated(userLocation.getUpdated());
            userLocation.setUpdated(System.currentTimeMillis());

            mongoTemplate.save(userLocation);
        }
    }

    @Override
    public List<Long> searchNear(Long userId, Integer distance) {
        //1.获取我的地址位置
        UserLocation userLocation = mongoTemplate.findOne(new Query(Criteria.where("userId").is(userId)), UserLocation.class);

        //2.创建条件对象(我的坐标为圆心,距离为半径,在这个圈里用户)
        //获取我的坐标(圆心)
        GeoJsonPoint point = userLocation.getLocation();

        //确定半径范围
        Distance radius = new Distance(distance / 1000, Metrics.KILOMETERS);

        //以中心点画个指定半径的圆
        Circle circle = new Circle(point,radius);

        //在圆范围中查找
        Query query = new Query(Criteria.where("location").withinSphere(circle));

        //3.查询userLocation表获取用户集合
        List<UserLocation> userLocations = mongoTemplate.find(query, UserLocation.class);

        //4.遍历集合,获取每个用户id存入idList
        List<Long> ids = userLocations.stream().map(UserLocation::getUserId).collect(Collectors.toList());

        //5.返回idList
        return ids;
    }
}
